package com.zhongshengnetwork.rightbe.lang.view;

import android.content.Context;
//import android.support.v7.widget.RecyclerView;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hys.utils.ImageUtils;
import com.zhongshengnetwork.rightbe.Constant.Constant;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.Filter.FilterTools;
import com.zhongshengnetwork.rightbe.lang.activity.LangPublishActivity;

import java.io.File;
import java.util.List;

/**
 * 图片显示Adapter
 * Created by kuyue on 2017/6/19 下午3:59.
 * 邮箱:595327086@qq.com
 */

public class PostArticleImgAdapter extends RecyclerView.Adapter<PostArticleImgAdapter.MyViewHolder> {

    private List<String> mDatas;
    private final LayoutInflater mLayoutInflater;
    private final Context mContext;
    private int imgW = 0;
    private int maxImgCount = 9;
    public int filterIndex = 0;
    public PostArticleImgAdapter(Context context, List<String> datas) {
        this.filterIndex = 0;
        this.maxImgCount = 9;
        this.mDatas = datas;
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(context);
        WindowManager wm = (WindowManager) this.mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;         // 屏幕宽度（像素）
        int height = dm.heightPixels;       // 屏幕高度（像素）
        float density = dm.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
        int densityDpi = dm.densityDpi;     // 屏幕密度dpi（120 / 160 / 240）
        imgW = (int) ((width-60*density)/4);
    }

    public PostArticleImgAdapter(Context context, List<String> datas, int imgCount) {
        this.filterIndex = 0;
        this.maxImgCount = imgCount;
        this.mDatas = datas;
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(context);
        WindowManager wm = (WindowManager) this.mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;         // 屏幕宽度（像素）
        int height = dm.heightPixels;       // 屏幕高度（像素）
        float density = dm.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
        int densityDpi = dm.densityDpi;     // 屏幕密度dpi（120 / 160 / 240）
        imgW = (int) ((width-60*density)/4);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(mLayoutInflater.inflate(R.layout.item_post_activity, parent, false));
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        if (position >= maxImgCount) {//图片已选完时，隐藏添加按钮
            holder.imageView.setVisibility(View.GONE);
        } else {
            holder.imageView.setVisibility(View.VISIBLE);
        }
        ViewGroup.LayoutParams params = holder.imageView.getLayoutParams();
        params.width =imgW;
        params.height = imgW;
        holder.imageView.setLayoutParams(params);
        if (filterIndex > 0 && mDatas.size() > 1){
            if (mDatas.get(position).contains(Constant.weijuTakePhoto) == true){
                Bitmap bitmap = ImageUtils.getBitmap(new File(CommonUtils.getFile(Environment.DIRECTORY_PICTURES),mDatas.get(position)));
                if (bitmap == null){
                    bitmap = ImageUtils.getBitmap(mDatas.get(position));
                }
                holder.imageView.setImageBitmap(FilterTools.toProcessImage(bitmap, filterIndex));
            }else {
                Bitmap bitmap = ImageUtils.getBitmap(mDatas.get(position));
                holder.imageView.setImageBitmap(FilterTools.toProcessImage(bitmap, filterIndex));
            }
        }else {
            if (mDatas.get(position).contains(Constant.weijuTakePhoto)){
                Bitmap bitmap = ImageUtils.getBitmap(new File(CommonUtils.getFile(Environment.DIRECTORY_PICTURES),mDatas.get(position)));
                if (bitmap == null){
                    Glide.with(mContext).load(mDatas.get(position)).diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate().into(holder.imageView);
                }else {
                    holder.imageView.setImageBitmap(ImageUtils.getBitmap(new File(CommonUtils.getFile(Environment.DIRECTORY_PICTURES),mDatas.get(position))));
                }
            }else {
                Glide.with(mContext).load(mDatas.get(position)).diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate().into(holder.imageView);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;

        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.sdv);
        }
    }


}
